﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace AcademicAudit_JoelShuman
{
	public class CSVio : StudentFileIO
	{
		public string GetStudentDirPath()
		{
			return "../../DataFiles/CsvStudents/";
		}

		public MainRecords ReadStudentFiles()
        {
            MainRecords theRecords = new MainRecords();
            var dirinfo = new DirectoryInfo(MainRecords.STUDENTDIR);
            var theFiles = dirinfo.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList();

            foreach (var file in theFiles)
            {
                theRecords.m_AllStudents.Add(CSVParser.ReadStudentCSV(file.FullName));
            }
            return theRecords;
		}

		public void ReadDefaultCourseFile()
		{
			DefaultCourseList.DEFAULTCOURSELIST = CSVParser.ParseCourseCSV("../../DataFiles/GenericCourseData.csv");
		}

		public void SaveToDisk(MainRecords theRecords)
        {
            foreach (StudentRecord thisStudent in theRecords.m_AllStudents)
            {
                CSVParser.WriteStudentCSV(thisStudent);
            }
		}

		public bool AddStudent(StudentRecord newStudent)
        {
            CSVParser.WriteStudentCSV(newStudent);

            if (!StudentIDNumbers.VerifyID(newStudent.m_StudentID))
            {
                MessageBox.Show(newStudent.m_StudentID + " is not a valid Student ID!\n\nUnable to create the student!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Delete the file.
                File.Delete(MainRecords.STUDENTDIR + newStudent.m_FileName + ".csv");
                StudentIDNumbers.FixIDFile();
                return false;
            }

            return true;
		}

        //public bool VerifyID(string theID)
        //{
        //    throw new NotImplementedException();
        //}

        //public void GetAllActiveIDs()
        //{
        //    throw new NotImplementedException();
        //}
	}
}
